<template>
	<view>
		<view class="cu-list menu sm-border card-menu margin-top">
			<view class="cu-item arrow">
				<view class="content" @tap="showModal" data-target="bottomModal">
					<text class="cuIcon-locationfill text-green"></text>
					<text class="text-grey">{{receiving_address}}</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">
					</text>
				</view>
			</view>
			<view class="cu-item arrow" @tap="coupon">
				<view class="content fontTexts" data-target="bottomModal">
					<view class="">
						<text class="cuIcon-ticket text-green"></text>
						<text class="text-grey youtext">优惠券</text>
					</view>
					<view v-if="coupon_concessions != 0" class="">
						<text class="text-black text-bold">-￥{{coupon_concessions}}</text>
					</view>

				</view>
				<view class="action">
					<text class="text-grey text-sm">
					</text>
				</view>
			</view>
			<view class="cu-item arrow" @click="remarks">
				<view class="content">
					<text class="cuIcon-formfill text-green"></text>
					<text class="text-grey">备注</text>
				</view>
				<view class="textareaValue">
					<text class="text-grey text_value">{{remarks_val}}</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">
					</text>
				</view>
			</view>
			<view class="cu-item cuItem_hei" v-for="(item,index) in ShoppingT" :key="index">
				<view class="content_pos">
					<view class="text-cut">{{item.mealName}}<text class="text-cut text-red">({{item.number}}份)</text></view>
					<view class="text-ms text-price text-red">{{item.total}}</view>
				</view>
				<view v-for="itex in item.restItems" class="radius lg restItemsImage">
					<image :src="htp + itex.itemImg" mode=""></image>
					<view class="text-cut text-center">{{itex.itemName}}X1</view>
				</view>
			</view>
			<view class="cu-item" v-for="(item,index) in ShoppingF" :key="index">
				<view v-for="itex in item.restItems" class="radius lg restItemsImage">
					<image :src="htp + itex.itemImg" mode=""></image>
				</view>
				<view class="content" v-for="itex in item.restItems">
					<view class="">
						<view class=""></view>
						<view class="text-cut">{{itex.itemName}}<text class="text-cut text-red">({{item.number}}份)</text></view>
					</view>
					<view class="action">
						<view class="text-ms text-price text-red">{{item.total}}</view>
						<view class=""></view>
					</view>
				</view>
			</view>
			<view class="blanks"></view>
		</view>
		<view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''" @tap.stop="">
			<view class="cu-dialog">
				<view class="cu-bar bg-white">
					<view class="action_texts text-black text-bold">选择收货地址</view>
					<view class="action text-blue" @tap="hideModal">取消</view>
				</view>
				<view class="cu-bar search bg-white">
					<view class="search-form round">
						<text class="cuIcon-search"></text>
						<input @input="onInput" :adjust-position="false" type="text" placeholder="搜索地址" confirm-type="search"></input>
					</view>
					<view class="action">
						<button class="cu-btn bg-green shadow-blur round">搜索</button>
					</view>
				</view>
				<view class="padding-xl colore">
					<view class="action_body" v-for="(item,index) in deliveryAddress" :key="index" @click="actionBody(item.companyName,item.id)">
						<text>{{item.companyName}}</text>
					</view>
					<!-- <view class="action_body" v-if="address == false" v-for="(item,index) in deliveryaddress" :key="index" @click="actionBody(index,item.id)">
						<text>{{item.companyName}}</text>
					</view> -->
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalNames=='DialogModal1'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">提示</view>
					<view class="action" @tap="cancel">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					您还没有登录，请先登录在下单
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn bg-green margin-left" @tap="determine">点击登录</button>

					</view>
				</view>
			</view>
		</view>
		<view class="cart">
			<image :src="srcImage" mode="widthFix"></image>
			<view class="text-price text-white fontText">{{totalPricex}}</view>
			<!-- <view v-if="concessions !== 0" class="text-white fontTexts">已优惠： {{concessions}} 元</view> -->
			<button class="cartBtn" @tap="submits">下单</button>
			<!-- <button type="default" @click="qingchu">qingchu</button> -->
		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				srcImage: "cloud://shiheji-5gkjlvup36de14c2.7368-shiheji-5gkjlvup36de14c2-1304241660/image/cart.png",
				nonceStr: null,
				package: null,
				signType: null,
				paySign: null,
				taocanstimeType: null, //套餐
				drinkstimeType: null, //饮品
				staplefoodstimeType: null, //主食
				streetfoodstimeType: null, //小吃
				SinglepointstimeType: null, //单点
				ShoppingCartstimeType: null, //轻食
				arrli: null,
				ShoppingT: null, //套餐
				ShoppingF: null, //非套餐
				timeType: null,
				total: 0,
				placeorderArr: [],
				drinksprice: null, //饮品价格
				ShoppingCartsprice: null, //轻食价格	
				Singlepointsprice: null, //单点价格	
				streetfoodsprice: null, //小吃价格	
				staplefoodsprice: null, //主食价格	
				drinks: null, //饮品
				ShoppingCarts: null, //轻食
				Singlepoints: null, //单点
				streetfoods: null, //小吃
				staplefoods: null, //主食
				SinglePointAmount: 0, //单点金额
				Singlepoint: null, //提交订单单点信息
				taocanArrs: '', //提交订单套餐信息
				tilists: [],
				totals: null,
				totalPrice: 0,
				modalName: null,
				modalNames: null,
				deliveryAddressres: '', //收货地址list
				deliveryAddressList: '',//收货地址list
				InputValue: null,
				ShoppingCartInformation: null, //查询购物车
				receiptTime: ''

			};
		},
		onShow() {
			this.selectShoppingCat()
		},
		onLoad(options) {
			this.receiptTime = options.time
		},
		created() {
			this.selectCustomerAddress(); //默认地址
		},
		mounted() {
			if (this.openid != ' ') {} else {
				this.modalNames = "DialogModal1"
				return false
			}
		},
		computed: {
			...mapGetters(['deliveryAddress']),
			...mapState([
				'openid',
				'id',
				'htp',
				'coupon_index',
				'coupon_concessions',
				'coupon_ids',
				'coupon_id',
				'remarks_val',
				'receiving_address',
				'companyId'
			]),
			//总价
			totalPricex() {
				let number = ''
				number = this.SinglePointAmount - this.coupon_concessions
				number = number.toFixed(2)
				this.total = number
				return number
			},
		},
		methods: {
			...mapMutations(['coupons', 'remarks', 'modify_shipping_address','receiving_address_list','Inputvlue']),
			// timeStamp() {
			// 	var timestamp = new Date().getTime();
			// 	console.log(timestamp)
			// 	this.randomString()
			// },
			determine(e) {
				this.modalNames = null
				uni.navigateTo({
					url: '../Login/login'
				})
			},
			cancel(e) {
				this.modalNames = null
				uni.navigateBack({
					delta: 1
				})
			},
			remarks() {
				uni.navigateTo({
					url: 'Remarks'
				})
			},
			//选择收货地址
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
				this.logincustomer() //获取收货地址
			},
			//取消
			hideModal(e) {
				this.modalName = null
			},
			//获取收货地址
			logincustomer() {
				this.request.logincustomer().then(res => {
					this.receiving_address_list(res.rows)
				})
			},
			//默认地址
			selectCustomerAddress() {
				let untidData = this.id.toString()
				this.request.selectCustomerAddress({
					oppenId: untidData
				}).then(res => {
					let address = res.restAddresName
					let address_id = res.restAddress
					this.modify_shipping_address({
						address: address,
						address_id: address_id
					})
				})
			},
			//收货地址点击事件
			actionBody(name, id) {
				this.modify_shipping_address({
					address: name,
					address_id: id
				})
				this.modalName = null
			},
			//输入框搜索地址
			onInput(e) {
				let Inputalue = e.detail.value
				Inputalue = Inputalue.toString()
				this.Inputvlue(Inputalue);
			},
			//查询购物车
			selectShoppingCat() {
				this.request.selectShoppingCat({
					oppenId: this.openid,
				}).then(res => {
					let length = res.length
					//购物车详细信息
					this.ShoppingCartInformation = res
					//购物车分类	
					//不是套餐的
					let ShoppingF = res.filter((item) => {
						return item.state !== "1"
					})
					this.ShoppingF = ShoppingF
					// 是套餐的
					let ShoppingT = res.filter((item) => {
						return item.state == "1"
					})
					this.ShoppingT = ShoppingT
					let taocan = res.filter(item => item.state == "1") //套餐
					let Singlepoint = res.filter(item => item.state == "2") //单点
					let ShoppingCart = res.filter(item => item.state == "3") //轻食					
					let streetfood = res.filter(item => item.state == "4") //小吃
					let staplefood = res.filter(item => item.state == "6") //其他	
					let drink = res.filter(item => item.state == "5") //饮品	
					this.taocanArrs = taocan
					//套餐
					let taocans = taocan.map((item) => {
						let price = item.primaryMoney
						let restItems = item.restItems
						let total = item.total
						let timeType = item.timeType
						let orderNumber = item.number
						let li = ''
						let arrli = []
						let foodId = ''
						let foodName = ''
						let stapleNumber = ''
						for (let i = 0, len = restItems.length; i < len; i++) {
							foodId = restItems[i].id
							foodName = restItems[i].itemName
							stapleNumber = 1
							if (foodId == 27) {
								stapleNumber = 0
							}
							li = {
								foodId,
								foodName,
								stapleNumber,
							}
							arrli.push(li)
						}
						let taocanarr = {
							price,
							total,
							arrli,
							timeType,
							orderNumber
						}
						return taocanarr
					})
					//饮品
					let drinks = drink.map((item) => {
						let li = {}
						let foodId = item.foodIds
						let foodName = item.mealName
						let stapleNumber = item.number
						this.drinkstimeType = item.timeType

						li = {
							foodId,
							foodName,
							stapleNumber,
						}
						return li
					})
					// 金额
					let drinkin = drink.map((item) => {
						return item.total
					})
					let drinkArr = drinkin.reduce((preVal, curVal) => {
						return Number(preVal) + Number(curVal)
					}, [])
					this.drinksprice = drinkArr
					//轻食
					let ShoppingCarts = ShoppingCart.map((item) => {
						let li = {}
						let foodId = item.foodIds
						let foodName = item.mealName
						let stapleNumber = item.number
						this.ShoppingCartstimeType = item.timeType
						li = {
							foodId,
							foodName,
							stapleNumber,
						}
						return li
					})
					// 金额
					let ShoppingCartin = ShoppingCart.map((item) => {
						return item.total
					})
					let ShoppingCartinArr = ShoppingCartin.reduce((preVal, curVal) => {
						return Number(preVal) + Number(curVal)
					}, [])
					this.ShoppingCartsprice = ShoppingCartinArr
					//单点
					let Singlepoints = Singlepoint.map((item) => {
						let li = {}
						let foodId = item.foodIds
						let foodName = item.mealName
						let stapleNumber = item.number
						this.SinglepointstimeType = item.timeType
						li = {
							foodId,
							foodName,
							stapleNumber,
						}
						return li
					})
					// 金额
					let Singlepoin = Singlepoint.map((item) => {
						return item.total
					})
					let SinglepoinArr = Singlepoin.reduce((preVal, curVal) => {
						return Number(preVal) + Number(curVal)
					}, [])
					this.Singlepointsprice = SinglepoinArr
					//小吃
					let streetfoods = streetfood.map((item) => {
						let li = {}
						let foodId = item.foodIds
						let foodName = item.mealName
						let stapleNumber = item.number
						this.streetfoodstimeType = item.timeType

						li = {
							foodId,
							foodName,
							stapleNumber,
						}
						return li
					})
					// 金额
					let streetfoodin = streetfood.map((item) => {
						return item.total
					})
					let streetfoodinArr = streetfoodin.reduce((preVal, curVal) => {
						return Number(preVal) + Number(curVal)
					}, [])
					this.streetfoodsprice = streetfoodinArr
					//主食
					let staplefoods = staplefood.map((item) => {
						let li = {}
						let foodId = item.foodIds
						let foodName = item.mealName
						let stapleNumber = item.number
						this.staplefoodsprice = item.total
						this.staplefoodstimeType = item.timeType
						li = {

							foodId,
							foodName,
							stapleNumber,
						}
						return li
					})
					// 金额
					let staplefoodin = staplefood.map((item) => {
						return item.total
					})
					let staplefoodinArr = staplefoodin.reduce((preVal, curVal) => {
						return Number(preVal) + Number(curVal)
					}, [])
					this.ShoppingCarts = ShoppingCarts //轻食
					this.Singlepoints = Singlepoints //单点
					this.streetfoods = streetfoods //小吃
					this.staplefoods = staplefoods //主食
					this.drinks = drinks
					this.taocanArrs = taocans
					let foodId = ''
					let foodName = ''
					let stapleNumber = ''
					let price = ''
					let li = []
					this.SinglePointAmount = 0
					for (let i = 0, len = res.length; i < len; i++) {
						foodId = res[i].total
						foodName = res[i].itemName
						stapleNumber = res[i].number
						price = res[i].total
						// 金额
						this.SinglePointAmount += price

						li.push({
							foodId,
							foodName,
							stapleNumber
						})
					}
					this.Singlepoint = li
				})
			},
			//优惠券
			coupon() {
				uni.navigateTo({
					url: 'coupon?money=' + this.SinglePointAmount
				})
			},
			//清除缓存
			ClearCache() {
				uni.clearStorageSync()
			},
			//去结算
			placeorder() {
				if (this.tilists == null) {
					// console.log("订单为空")
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: "订单为空,请先添加订单",
					})
					return false
				}
				let orderDTO = {
					"receiptTime": this.receiptTime,
					"price": this.totalPrice, //单菜品价格
					"mealClassName": this.mealClassName, //订单名称
					"mealClassCode": this.mealClassCode, //订单编号
					"controId": this.coupon_id, //优惠券Id          
					"bak": this.remarks_val, //备注
					"orderNumber": this.totals, // 订单数量
					"companyId": Number(this.companyId), // 公司id
					"totalAmount": this.total, // 总价
					"userId": this.openid, // oppenid 	
					"orderDetails": this.tilists, //	菜品以及菜品数量
					"timeType": this.timeType,
					"discont": this.concessions
				}
				this.placeorderArr.push(orderDTO)
			},
			submits() {
				this.placeorderArr = []
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				if (this.companyId == '') {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: "请先添加收货地址",
					})
					return false
				} else {
					if (this.taocanArrs == '') {} else {
						let total = 0;
						let arrData = this.taocanArrs
						for (let i = 0, len = arrData.length; i < len; i++) {
							this.totalPrice = arrData[i].price
							this.totals = arrData[i].orderNumber
							this.timeType = arrData[i].timeType
							this.tilists = arrData[i].arrli
							this.mealClassCode = 1
							this.mealClassName = "套餐"
							this.placeorder()
						}

					}
					if (this.ShoppingCarts == '') {} else {
						this.mealClassCode = 3
						this.timeType = this.ShoppingCartstimeType
						this.mealClassName = "轻食"
						this.tilists = this.ShoppingCarts
						this.totalPrice = this.ShoppingCartsprice
						this.totals = 1
						this.placeorder()
					}
					if (this.Singlepoints == '') {} else {
						this.mealClassCode = 2
						this.timeType = this.SinglepointstimeType
						this.mealClassName = "单点"
						this.tilists = this.Singlepoints
						this.totalPrice = this.Singlepointsprice
						this.totals = 1
						this.placeorder()

					}
					if (this.streetfoods == '') {} else {
						this.mealClassCode = 4
						this.timeType = this.streetfoodstimeType
						this.mealClassName = "小吃"
						this.tilists = this.streetfoods
						this.totalPrice = this.streetfoodsprice
						this.totals = 1
						this.placeorder()

					}
					if (this.staplefoods == '') {} else {
						this.mealClassCode = 3
						this.timeType = this.staplefoodstimeType
						this.mealClassName = "主食"
						this.tilists = this.staplefoods
						this.totalPrice = this.staplefoodsprice
						this.totals = 1
						this.placeorder()

					}
					if (this.drinks == '') {} else {
						this.mealClassCode = 5
						this.timeType = this.drinkstimeType
						this.mealClassName = "饮品"
						this.tilists = this.drinks
						this.totalPrice = this.drinksprice
						this.totals = 1
						this.placeorder()

					}

				}
				this.addSave()
			},
			//生成订单
			addSave() {
				let total = this.total * 100
				total = total.toFixed(2)
				total = Number(total)
				let resDatase = null
				let openId = this.openid;
				let placeorderArr = this.placeorderArr
				if (placeorderArr == '') {
					return false
				}
				this.request.addSave(JSON.stringify({
					"oppenId": openId, // oppenid 	
					"total": total,
					"orderDTO": placeorderArr
				})).then(res => {
					if (res.code == 0) {
						this.timeStamp = res.data.payTimeStamp
						this.nonceStr = res.data.paynonceStr
						this.package = res.data.partnerId
						this.signType = res.data.paySignType
						this.paySign = res.data.paySign
						uni.hideLoading()
						this.wxPays()
					}
					if (res.code == 500) {
						uni.showToast({
							icon: 'none',
							title: res.msg,
							duration: 2000
						})
						return false
					}
				})
			},
			deleteAll() {
				this.request.deleteAll({
					oppenId: this.openid
				}).then(res => {})
			},
			wxPays() {
				let timeStamp = this.timeStamp
				let nonceStr = this.nonceStr
				let packages = this.package
				let signType = this.signType
				let paySign = this.paySign
				wx.requestPayment({
					timeStamp: timeStamp,
					nonceStr: nonceStr,
					package: packages,
					signType: "RSA",
					paySign: paySign,
					success: (res) => {},
					fail: (err) => {},
					complete: (res) => {
						if (res.errMsg == "requestPayment:ok") {
							if (res.reqId !== '') {
								if (this.coupon_ids != '') {
									this.request.controller({
										ids: this.coupon_ids
									}).then(res => {})
								}
							}
							this.coupons({
								coupon_index: -1,
								coupon_id: '',
								coupon_concessions: 0,
								coupon_ids: ''
							})
							uni.setStorage({
								key: 'deliveryNames',
								data: ''
							})
							if (this.openid != '') {
								this.deleteAll()
							}
							this.remarks(' ')
							setTimeout(function() {
								wx.reLaunch({
									url: '../Home/Refund'
								})
							}, 200)
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.cart {
		display: table-cell;
		vertical-align: middle;
		z-index: 1000;
		width: 96%;
		position: fixed;
		bottom: 40rpx;
		height: 90rpx;
		line-height: 90rpx;
		background: #f37b1d;
		margin: auto;
		border-radius: 40rpx;
		left: 2%;
	}

	.cart image {
		width: 160rpx;
		position: absolute;
		bottom: 0;
		left: 5%;
	}

	.cartBtn {
		height: 90rpx;
		line-height: 90rpx;
		z-index: 1000;
		border-radius: 0 39rpx 39rpx 0;
		font-size: 30rpx;
		width: 200rpx;
		background: #39B54A;
		position: absolute;
		right: 0;
		color: #FFFFFF;
	}

	.fontText {
		position: absolute;
		left: 26%;
		font-size: 48rpx;
	}

	.fontTexts {
		display: flex;
		justify-content: space-between;
		padding-left: 12rpx;
	}

	.action_body {
		height: 96rpx;
		font-size: 35rpx;
		line-height: 96rpx;
		border-bottom: 1rpx solid #F2F2F2;
	}

	.colore {
		background-color: #FFFFFF;
	}

	.cu-list.menu>.cu-item {
		padding: 20px 30rpx;
	}

	.cu-avatar {
		margin-right: 30rpx;
	}

	.cu-item {
		line-height: 58rpx;
	}

	.blanks {
		height: 130rpx;
	}

	.action_texts {
		font-size: 32rpx;
		width: 70%;
		margin-left: 120rpx;
		text-align: center;
		position: absolute;
	}

	.textareaValue {
		text-align: right;
		max-width: 436rpx;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
	}

	.restItemsImage {
		background-color: none;
		margin-right: 20rpx;
		width: 106rpx;
		height: 96rpx;

		image {
			border-radius: 11rpx;
			height: 100%;
			width: 100%;
		}
	}

	.cuItem_hei {
		height: 310rpx;
	}

	.content_pos {
		position: absolute;
		top: 34rpx;
		width: 90%;
		display: flex;
		justify-content: space-between;
	}

	.youtext {
		margin-left: 14rpx;
	}

	.cu-dialog {
		position: relative;
		display: inline-block;
		vertical-align: middle;
		margin-left: auto;
		margin-right: auto;
		width: 680rpx;
		height: -webkit-fill-available;
		max-width: 100%;
		background-color: #f8f8f8;
		border-radius: 10rpx;
		overflow: hidden;
	}
</style>
